package com.hdu.redis;

import com.hdu.context.FullLinkContext;
import com.hdu.context.FullLinkContextHolder;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class KeyStringRedisSerializer extends StringRedisSerializer {

    private final RedisShadowKeyConfiguration redisShadowKeyConfiguration;

    public KeyStringRedisSerializer(RedisShadowKeyConfiguration redisShadowKeyConfiguration) {
        this.redisShadowKeyConfiguration = redisShadowKeyConfiguration;
    }

    @Override
    public byte[] serialize(String redisKey) {
        FullLinkContext fullLinkContext = FullLinkContextHolder.getFullLinkContext();
        if (fullLinkContext != null) {
            // 全链路压测数据, 添加key前缀 or 后缀
            redisKey = redisShadowKeyConfiguration.getKey(redisKey);
        }
        return super.serialize(redisKey);
    }
}
